package com.changfeng.controller;

import com.changfeng.bean.dto.vo.AdminBannerListVO;
import com.changfeng.bean.dto.vo.IndexBannerListVO;
import com.changfeng.exception.ChangFengException;
import com.changfeng.exception.ChangFengExceptionEnum;
import com.changfeng.service.BannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @PostMapping("/bannerSave")
    public void saveBanner(@RequestBody AdminBannerListVO vo) {
        if (vo == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        if (vo.getBannerId() == null) {
            bannerService.saveBanner(vo);
        } else {
            bannerService.updateBanner(vo);
        }
    }

    @GetMapping("/findAll")
    public List<AdminBannerListVO> findAll() {
        return bannerService.findAll();
    }

    @GetMapping("/getBanner")
    public AdminBannerListVO getBanner(Integer bannerId) {
        if (bannerId == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        return bannerService.findById(bannerId);
    }

    @GetMapping("/deleteBanner")
    public void deleteBanner(Integer bannerId) {
        if (bannerId == null) {
            throw new ChangFengException(ChangFengExceptionEnum.PARAM_EXCEPTION);
        }
        bannerService.deleteBanner(bannerId);
    }

    @GetMapping("/findAllIndex")
    public List<IndexBannerListVO> findAllIndex(){
        return bannerService.findIndexAll();
    }


}
